package assignment;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;


/**
 * @author $Author:$
 * @version $Id: $
 */
public class UrlFetcher {

	public UrlFetcher(){
		
	}
	
	/**
	 * Retrieves the data within a web page
	 * @param url the URL of the web page data
	 * @return The retrieved data
	 * @throws IOException 
	 * @throws MalformedURLException 
	 */
	public StringBuffer get(String url) throws MalformedURLException, IOException{
		System.out.println("Fetching: "+url);

		// Define and open a connection to the web page to retrieve
		URLConnection con = new URL(url).openConnection();
		
		// Define the reader and a container for the data to be retrieved
		InputStreamReader in = new InputStreamReader(con.getInputStream());
		StringBuffer out = new StringBuffer();
		
		// Define auxiliary variables
		char[] buffer = new char[8*1024];
		int readed = 0;
		
		// Retrieve data
		while((readed=in.read(buffer))!=-1){
			out.append(buffer, 0, readed);
		}
		
		return out;
	}
}
